#! /bin/bash
## English version
### 	WARNING THIS SCRIPT IS FOR SUPERUSER ONLY and TERMINAL CONSOL    ###

#  NVIDIA-MAKER
#  Sat Feb  6 16:58:20 2010
#  Copyright  2010  mk67
#  <rmk677@gmail.com>

#  This program is free software; you can redistribute it and/or modify
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License, or (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#  
#  You should have received a copy of the GNU Lesser General Public
#  License along with main.c;if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA

cd /
### VARS
### -------------------------------------------------------
## Master Vars.
nvdir="/usr/local/src64/NVIDIA"
nvtar="$nvdir/tgz"					# archies directory
nvtmp="$nvdir/temp"					# install temp directory
nvlog="$nvdir/log"					# logs directory
nvdl="$nvdir/release"					# downlaod driver backups directory
nvupdate="$nvdir/update"				# update temp directory
logfile="--log-file-name=$nvlog/install.log"		# nvidia-installer options
temp="--tmpdir=$nvtmp"					# nvidia-installer option: install temp dir
dltemp="--tmpdir=$nvupdate"				# nvidia-installer option: update temp dir


## Script colours Vars.
t='\e[0;39m'	#default terminal colour
r='\e[1;31m'	#red
v='\e[1;32m'	#green
y='\e[1;36m'	#cyan
j='\e[1;33m'	#yellow


### VARS
ID(){
## card id 
board=$(lspci -nn | grep 'VGA' | awk '{ print $9" "$10" "$11" "$12" "$13 }' | tr '[[]]' ' ')
## vendor
vendor=$(lspci -nn | grep 'VGA' | awk '{ print  $1" "$6" "$7 }')
}
## system arch
arch(){
ARCH=`uname -a | awk '{ print $12 }'`
if [ $ARCH == "i686" ] || [ $ARCH == "i586" ] ; then
	ARCH="x86"
	RUN_PCK="pkg1"
else
	RUN_PCK="pkg2"
fi
}
## system distro --> need progress
function distro(){
if [ -f /proc/version ] ; then
	version="cat /proc/version"
	cat /proc/version | grep ubuntu > /dev/null
	if [[ $? -eq 0 ]]; then
	DISTRO="UBUNTU"
	PKG_INSTALLER="apt-get install"
	version=$( uname -r | cut -d '-' -f 1 )
	PKG="lftp kernel-headers-$version gcc xterm"
	fi
	cat /proc/version | grep mandriva > /dev/null
	if [[ $? -eq 0 ]]; then
	DISTRO="MANDRIVA"
	PKG_INSTALLER="urpmi"
	desk=$( uname -r | cut -d '-' -f 2 )
	kernel=$( uname -r | cut -d '-' -f 1 )
	version=$( uname -r | cut -d '-' -f 3 )
	PKG="lftp kernel-$desk-devel-$kernel-$version gcc xterm"
	fi
else
	echo -e "\f$r $msg101 " ###
fi
}
# dependencies control
function dep_control(){
	ls /usr/bin/lftp > /dev/null
	if [ $? -ne 0 ] ; then 
		echo -e "$v Required dependencies are not installed "; sleep 2 ###
		echo -e "$y INSTALL RUNNING..." ###
		$PKG_INSTALLER $PKG
		echo -e "$y INSTALL DONE   " ###
	fi
}
# Internet connection control function.
router_ip(){
	wget -O - -q icanhazip.com
}
ping_net(){
if [[ "$(ping -q -c 1 nvidia.com | sed -n '4p' | awk '{print $1}')" == 1 ]] ; then
	connection="$msg116"
else
	connection="$msg117"
fi 
}
function ip_net(){
	#question="eth0,eth1,ppp0,ppp1,wlan0,wlan1"
	question=$(/sbin/ifconfig | sed -n '/lo/d;/^ /g;/ $/p' | awk '{print $1}')
	#echo "$question" | tr ',' '\n' > $EXEC/net_list
	for net in $question ; do #$(cat $EXEC/net_list) ; do 
		
		if [ "$(/sbin/ifconfig $net | sed -n '/lo/d;/^ /g;/ $/p' | awk '{ print $1 }')" != '' ] ; then
		#ip_check=$(  )
			if [[ "$(/sbin/ifconfig $net | awk '{ print $2 }' | sed -n 2p | cut -d ':' -f 2 | grep [[:digit:]])" != '' ]]; then 
				net_ip=$(/sbin/ifconfig $net | awk '{ print $2 }' | sed -n 2p | cut -d ':' -f 2)
				if [[ $(router_ip) != $net_ip ]] ; then
					ip=$(router_ip)
				else
					ip=$net_ip
				fi
				iface=$net
			fi
		fi
	done
#		net=$net_check
#		ip=$ip_check
}
# Primary control of X and network connection.
function xlocksession(){
	if [ -e /tmp/.X0-lock ] ; then				# TEST >> if [[ `runlevel | awk '{print $2}'` == 5 ]] ; then
		echo -e "\f$y  $msg104$t\f"
		echo -e "\f$v  $msg105 \n$msg114\n$msg115$t\f"
		ip_net
		if [[ "$(echo $ip)" == 127.* ]] || [[ "$(echo $ip)" == '' ]]; then
			echo -e "$r  $msg106$t\f"
		else
		echo -e "\f$v $msg107$j $qst001 $v:\n ==> $j\c"
		read -n 1 wilu ; echo -e "$v"
			if [ "$wilu" == "$ans001" ] ; then
			echo -e "$y \f $msg108"
			echo -e "$v\r\\t $qst003 " ### question 003
				read -s -n 1 confirm
				if [ $confirm == Q ] ; then
					menu
				else
					if [ $confirm == C ] ; then
					echo -e "\t $msg110 ...." ; sleep 2
					/usr/sbin/gdm-stop
					fi
				fi
			fi
		#sleep 1 ; exit 1
		#menu
		fi		
	fi
}
# making links and originals libraries
function rmlink(){
	function librmlk(){
		rm -vf $class/libGL.*
		rm -vf $class/libGLcore.*
		rm -vf $class/libcuda.*
		rm -vf $class/libXvMCNVIDIA.*
		rm -vf $class/libnvidia-cfg.*
		rm -vf $class/libnvidia-tls.*
		rm -vf $class/libvdpau.so.*
		rm -vf $class/libvdpau_nvidia.so.*
		rm -vf $class/libvdpau_trace.so.*
	}
	function xorgrmlk(){
		rm -vf $class/xorg/modules/libnvidia-wfb.*
		rm -vf $class/xorg/modules/extensions/libglx.*
	}
	function tlsrmlk(){
		rm -vf $class/tls/libnvidia-tls.*
	}
	lib64="/usr/lib64"
	lib32="/usr/lib"
	if [[ $(uname -a | awk '{ print $15 }') -eq 64 ]] ; then
		#for lib64 in rmlink ; do
		class=$lib64
		librmlk ; xorgrmlk ; tlsrmlk
		#done
		#for lib32 in rmlink ; do
		class=$lib32
		librmlk ; tlsrmlk
		#done
	else
		if [[ $(uname -a | awk '{ print $15 }') != 64 ]] ; then
			class=$lib32
			librmlk ; xorgrmlk ; tlsrmlk
		fi
	fi
}
# Interanl archive links creation
function makelink(){
	function usrlink(){
		ln -s -f -T $class/libGL.so.* $class/libGL.so.1
		ln -s -f -T $class/libGL.so.1 $class/libGL.so
		ln -s -f -T $class/libGLcore.so.* $class/libGLcore.so.1
		ln -s -f -T $class/libcuda.so.* $class/libcuda.so.1
		ln -s -f -T $class/libcuda.so.1 $class/libcuda.so
		ln -s -f -T $class/libnvidia-cfg.so.* $class/libnvidia-cfg.so.1
		ln -s -f -T $class/libnvidia-cfg.so.1 $class/libnvidia-cfg.so
		ln -s -f -T $class/libnvidia-tls.so.* $class/libnvidia-tls.so.1
		ln -s -f -T $class/libvdpau.so.* $class/libvdpau.so.1
		ln -s -f -T $class/libvdpau.so.1 $class/libvdpau.so
		ln -s -f -T $class/libvdpau_nvidia.so.* $class/libvdpau_nvidia.so
		ln -s -f -T $class/libvdpau_trace.so.* $class/libvdpau_trace.so
	}
	function xorglink(){
		ln -s -f -T $class/libXvMCNVIDIA.so.* $class/libXvMCNVIDIA_dynamic.so.1
		ln -s -f -T $class/libXvMCNVIDIA_dynamic.so.1 $class/libXvMCNVIDIA_dynamic.so
		ln -s -f -T $class/xorg/modules/libnvidia-wfb.so.* $class/xorg/modules/libnvidia-wfb.so.1
		ln -s -f -T $class/xorg/modules/extensions/libglx.so.* $class/xorg/modules/extensions/libglx.so
	}
	function tlslink(){
		ln -s -f -T $class/tls/libnvidia-tls.so.* $class/tls/libnvidia-tls.so.1
	}
	lib64="/usr/lib64"
	lib32="/usr/lib"
	if [[ "$(uname -a | awk '{ print $15 }')" -eq "64" ]] ; then
		class=$lib64
		usrlink ; tlslink ; xorglink ; sleep 1
		class=$lib32
		usrlink ; tlslink ; sleep 1
	else
		if [[ "$(uname -a | awk '{ print $15 }')" != "64" ]] ; then
			class=$lib32
			usrlink ; tlslink ; xorglink ; sleep 1
		fi
	fi
}
# Directories and files listing.
function list(){
		#listdir="$tmplist"
		wlist=`ls -d $listdir$opt1$opt2$opt3 | sed 's|'$optlist'|--->  |' | cat -n`
		echo "$wlist"
		echo -e "\r$v $ans002\n ==>  $j\c"
		read list
		result=`echo "$wlist" | sed -n ''$list'p'`
		#echo ${result#*--> }
}
## Some Other Vars.
# Check Installed version
# elf type
function libclass(){
	if [[ "$(uname -a | awk '{ print $15 }')" == "64" ]] ; then
		ELF_TYPE="64"
	else
		ELF_TYPE=""
	fi
}
function version_id(){
	libclass
	version=$(echo /usr/lib$ELF_TYPE/xorg/modules/extensions/libglx.so.*)
	echo ${version#*so.}
}
# check aviable updates
function check_update(){
ping_net
ip_net
if [[ "$(echo $ip)" == 127.* ]] || [[ "$(echo $ip)" == '' ]]; then
	echo -e "\f$r $msg411$connection"
	echo -e "$v $msg418\n $msg417$t"
	menu
else
	if [[ $net_ip != $ip ]] ; then
		echo -e "\f$v $msg111$connection $msg118 $j$( echo $iface )$v.\n $msg119 $msg112$j$(echo $ip)$v, $msg113 $msg112$j$(echo $net_ip)$v.\n"
	else
		echo -e "\f$v $msg111$connection $msg118 $j$( echo $iface )$v.\n $msg113 $msg112$j$(echo $ip)$v.\n"
	fi
	lftp -c "anon; cd ftp://download.nvidia.com/XFree86/Linux-$ARCH/ ; ls > $nvtmp/drvlist ; cat latest.txt > $nvtmp/last_update ; quit"
	LAST_IN=$(version_id)
	LAST_DRV=$(cat $nvtmp/last_update | awk '{ print $1 }')
	LAST_BETA=$(cat $nvtmp/drvlist | awk '{ print $9 }' | sort -gr | sed -n 1p)
	echo -e "\n\t$v $msg201$y$board$v:" 
	echo -e "\n\t$v $msg202\t$j$LAST_IN "
	echo -e "\n\t$v $msg203\t$j$LAST_DRV "
	echo -e "\n\t$v $msg204\t\t$j$LAST_BETA "
fi
}
### ARCHIVED DRIVERS INSTALL FUNCTION.
function from_archive(){
	echo -e "$v $msg220$y\r"
	cd $nvtar
		listdir="$nvtar"
		opt1="/*"
		optlist="${listdir#*}"
	list
	drivertar=${result#*-->  /}
	echo -e "\n$v $msg221$j ${result#*-->  /}."
	echo -e "\n$r --->$y     $msg222$r     <---$y\r"
	cd /
	echo -e "\n$j $msg223...$y"
	rmlink
	cp -T `tar xvf $nvtar/$drivertar` /
	echo -e "\n$j $msg224... $y\n"
	makelink
	libclass
	if [ -f /usr/lib$ELF_TYPE/xorg/modules/extensions/libglx.so.$(version_id) ] ; then
		ls -l /usr/lib$ELF_TYPE/xorg/modules/extensions/libglx.so.$(version_id) | cut -d ' ' -f 6,7 | sed -n "s/2 / $(echo $msg231) /;s/^/$(echo $msg230) /p"
		echo -e "\n$r >>$v $msg225$r<<"
	else
		echo -e "\n$v >>$r $msg226$v<<"
		echo -e "\n$v $msg227 "
		menu
	fi
	echo -e "\n$v $msg228"
	echo -e "$v $qst002\n$j $qst1$r ==>$j \c"
	read -n 1 dmrestart
		if [ $dmrestart == "$ans001" ] ; then
			echo -e "$v $msg229\r" ; sleep 2
			modprobe -r nvidia ; depmod -qa
			modprobe nvidia ; dm_restart
			#break
		else
			echo -e "$v\n" ; menu
		fi
}

### FROM A USER DIRECTORY INSTALL FUNCTION.
function from_directory(){
	function installdrv(){
		if [[ -s $driverep ]] ; then
			echo -e "\n$r --->$v     $msg301$r     <---$y\r"
			sh $driverep -a -n -q --ui=none $temp $logfile
			echo -e "$j $msg302\n$y" ; sleep 2
			cd $nvtmp ; sh $driverep -x
			nvlv=`$nvdir/nvidia-installer --latest --ui=none | sed -n '5p' | awk '{ print $4 }'`			
			#drv_actu=`ls -D | grep '${nvlv%;*}'`		# liste les Répertoire NVIDIA présent
			if [ -x $driverep ] ; then
				#cp -f $driverrep/nvidia-installer $nvdir
				cp -f $driverep $nvdl/nv-update-${nvlv%;*}
				if [[ -f $nvdl/nv-update-${nvlv%;*} ]] ; then
					echo -e "\n$v $msg303$j ${nvlv%;*}$v $msg305\,\n $msg304.\n"
					sleep 1
				else
					echo -e "\n$v $msg303$j ${nvlv%;*}$v $msg306."
					echo -e "\n$v $msg307"
					echo -e "\n$v $msg309"
					echo -e "\n$v $msg309"
					read -n 1 ok
				fi				
			fi
			archivage
		else
			echo -e "\n$v $msg310 \n"
			echo -e "\n$v $msg311$y http://www.nvidia.fr/Download/Find.aspx?lang=en$v or to the NVIDIA ftp repos,"
			echo -e "\n$v $msg311$y ftp://download.nvidia.com/XFree86/"
			echo -e "\n$v $msg312$t\n"
			echo -e "\n$v $msg313 $j"
			select answer in "$msg314" "$msg315" ; do
				case $answer in
					"$msg314")
						menu
						;;
					"$msg315")
						dm_restart
						;;
				esac
			done
		fi
	}
	echo -e "$v $msg320$y\n"
	ls $nvdl
	echo -e "\n$v $msg321$j\n"
		VP=" $msg322"
		RU=" $msg323"
	select repo in "$VP" "$RU" ; do
		case $repo in
			"$VP")
				echo -e "$y" ; cd $nvdl
				echo -e "$v $msg324 $nvdl : $y"
				listdir="$nvdl"
				opt1="/*"
				opt3=""
				optlist="${listdir#*}"
				list
				driver="${result#*-->  /}" 
				driverep="$nvdl/$driver" ; installdrv
			;;
			"$RU")
				# niveau primaire
				echo -e "$y" ; cd /home
				listdir="/home"
				opt1="/*"
				opt3="/"
				optlist="${listdir#}"
				list
				homerep="${result#*-->  $opt3}" ; echo $homerep
				cd $homerep
				echo -e "\n$v $msg325$j $homerep$y," ; sleep 1
				ls  -d /home/$homerep/* | sed 's|/home/'${homerep#*}'|--->  |'
				echo -e "\n$v $msg326"
				echo -e "$v ==> $j\c"
				read -e driver
				driverep="/home/$homerep/$driver"
				chmod a+x $driverep
				installdrv
			;;
		esac
	done
}

### UPDATE FUNCTION, FROM INTERNET.
function from_net(){
# download functions 
	package_list(){
		pck_drv=$(cat $nvtmp/drvlist | awk '{ print $9 }' | sort -g | sed -n 's/^/--> /p' | cat -n)
		echo -e "$y$pck_drv"
		echo -e "\n$v $msg420"
		echo -e "$v $ans002\n ==>  $j\c"
		read pck
		PICK_DRV=$(cat $nvtmp/drvlist | awk '{ print $9 }' | sort -g | sed -n ''$pck'p')
		echo -e "\n$v $msg421 $j$PICK_DRV" ; sleep 1
	}
	last_pack(){
		echo -e "\n$r --->$v     $msg401$r $y\r"
		wget -c --progress=bar:force ftp://download.nvidia.com/XFree86/Linux-$ARCH/$LAST_PACK/*$RUN_PCK* $nvupdate/
		echo -e "\n$r --->$v     $msg402$r $y\r" ; sleep 1
		echo -e "\n$r --->$v     $msg403$r $y\r" ; sleep 1
	}
	installe(){
		#nvr=`$nvi | sed -n '6,$p'`
		nwdrv=$(ls $nvupdate | grep $LAST_PACK) ; echo "$nwdrv"
		sh $nwdrv -a -q -n --ui=none $dltemp $logfile
		# pre achiving
		if [[ $(ls $nvupdate/$nwdrv) ]] ; then
			sh $nvupdate/$nwdrv -x
			cd ../
		else
			echo -e "\n$j $msg404\n "
			sleep 1 ; menu
		fi
		drvrep=`ls -D | grep '$LAST_PACK'`		# NVIDIA directory listing
		cp -f $nvupdate/$drvrep/nvidia-installer $nvdir
		mv -T $nvupdate/$nwdrv $nvdl/nv-update-$LAST_PACK
		mv -f $nvupdate/* $nvtmp/
		chmod 755 $nvtmp/*
	}
# script
#if [[ "$net_ip" == 127.* ]] ; then
#	echo -e "\f$v $msg411$t"
#	menu
#else
ping_net
ip_net
#	echo -e "\f$v $msg111 $(echo $net_ip) $msg112 $(echo $net_int) $msg113"
	if [ -e $nvdir/nvidia-installer ] ; then
		cd $nvupdate
		# le binaire Nvidia à un peu de mal avec l'exec après transfert,
		# aidons-le un peu.
		check_update
		echo -e "\n\t$v $msg412\n\t $msg422\n\t $msg423"
			read -s -n 1 confirm2
			if [ $confirm2 == C ] ; then
				echo -e "$v\n $msg413$j\r"
				A1="$LAST_DRV"
				A2="$LAST_BETA"
				A3="$msg419"
				PS3="`echo -e "\n$v $ans002$j ==>"` "
				select choice4 in "$A1" "$A2" "$A3"; do 
					case $choice4 in
						"$A1")
							echo -e "$v--->  $msg414...  <---\r"
							LAST_PACK=$LAST_DRV
							last_pack
							installe
							#rm -f $nvtmp/drvlist $nvtmp/last_up
							archivage
						;;
						"$A2")
							echo -e "$v--->  $msg415...  <---\r"
							LAST_PACK=$LAST_BETA
							last_pack
							installe
							#rm -f $nvtmp/drvlist $nvtmp/last_up
							archivage
						;;
						"$A3")
							echo -e "$v--->  $msg413...  <---\r"
							package_list
							LAST_PACK=$PICK_DRV
							last_pack
							installe
							#rm -f $nvtmp/drvlist $nvtmp/last_up
							archivage
						;;
					esac
				done
			else
				if [ $confirm2 == M ] ; then
					rm -f $nvtmp/drvlist $nvtmp/last_up
					sleep 1 ; menu
				fi
			fi
	else
		echo -e "$v $msg416"
		sleep 1 ; $B
	fi
#fi
}



### ARCHIVING. ###
function drivercall(){
	ls $nvtmp | grep $(version_id)
} 
# sub function: METHOD CHOICE
function archivage(){
echo -e "$v $msg501\n $qst002$j $qst001$v:"
	echo -e "\n$v ==>$j \c" ; read -n 1 archive
	case $archive in
		Y|y|O|o)
			echo -e "\n$v $msg502..." ; sleep 1
			echo -e "$y $(ls -d $nvtmp/$(drivercall))"
			cd $nvtmp
			if [ -f $(ls $nvtmp | grep $(version_id))/LICENSE ] ; then
				echo -e "\n$v\"$j $(drivercall) $v\" $msg503\n$y"
			else
				echo -e "\n$v $msg504"
				sleep 1
			fi
			archive ; sleep 1
			echo -e "$v\n  $msg505" ; sleep 1
	 		echo -e "$v\n $msg506\n"
			rm -f -R $nvtmp/*
			sleep 2 ; menu
			;;
		N|n)
			rm -f -R $nvtmp/*
			echo -e "$v\n $msg507" ; sleep 1
			echo -e "$v\n $msg508"
	 		echo -e "$v\n $msg509\n"
			sleep 2 ; menu
	 		;;
	esac
}

### REAL ARCHIVING
function archive(){
cd $nvtmp
## archive function
 	function do_tar(){
		if [[ -d $nvtmp/$nvrep ]] ; then
			echo -e "\n$r>>$j $msg511 $(version_id). $r <<$y\n" ; sleep 2
		else
			echo -e "\n$r $msg512"
			echo -e "\n$v $msg513$y\n" ; sleep 1
			rm -f -R $nvtmp/* ; menu
		fi
		tmpdir=$(version_id)
		mkdir  -v -p $tmpdir
		if [ -d $nvtmp/$tmpdir ] && [ -d $nvtmp/$nvrep ] ; then
				mkdir -p $tmpdir/lib/modules/`uname -r`/kernel/drivers/video
				mkdir -p $tmpdir/usr/lib
				mkdir -p $tmpdir/usr/lib/tls
				mkdir -p $tmpdir/usr/bin
				mkdir -p $tmpdir/usr/include
				mkdir -p $tmpdir/usr/share
				cp /lib/modules/`uname -r`/kernel/drivers/video/nvidia.ko $tmpdir/lib/modules/`uname -r`/kernel/drivers/video/
				cp $nvrep/usr/bin/* $tmpdir/usr/bin/
				cp  -R $nvrep/usr/include/* $tmpdir/usr/include/
				cp  -R $nvrep/usr/share/* $tmpdir/usr/share/
			if [[ `uname -a | awk '{ print $15 }'` == 64 ]] ; then
				mkdir -p $tmpdir/usr/lib64/xorg/modules
				mkdir -p $tmpdir/usr/lib64/tls
				cp $nvrep/usr/X11R6/lib/* $tmpdir/usr/lib64/
				cp  -R $nvrep/usr/X11R6/lib/modules/* $tmpdir/usr/lib64/xorg/modules/
				cp  -R $nvrep/usr/lib/* $tmpdir/usr/lib64/
				cp  -R $nvrep/usr/lib32/* $tmpdir/usr/lib/
			else
				mkdir -p $tmpdir/usr/lib/xorg/modules
				cp  $nvrep/usr/X11R6/lib/* $tmpdir/usr/lib/
				cp  -R $nvrep/usr/X11R6/lib/modules/* $tmpdir/usr/lib/xorg/modules/
				cp  $nvrep/usr/lib/* $tmpdir/usr/lib/
			fi
			cd $tmpdir ; tar cvf $nvtar/$tmpdir-`uname -r`.tar.gz ./lib ./usr	
			cd $nvdir ; rm -R $nvtmp/*
			echo -e "\n$v  $msg514$j $tmpdir-`uname -r`"
			sleep 1
		else
			echo -e "$v $msg515 "
			echo -e " $msg516"
			rm -f -R $nvtmp/* ; menu
		fi
	}

echo -e " $(drivercall)"
# actual driver version before pkg run extraction
	if [ -f $nvdl/nv-update-$(version_id) ] ; then
		pkg="`ls $nvdl | grep $(version_id)`" 
		echo -e "$y $msg521$j $(version_id)."
		echo -e "$y $msg522$j ${pkg#*te-}.$y"
		if [[ -d $(drivercall) ]] ; then
			echo -e "$r $msg523"
			sleep 1
		else
			echo -e "$r $msg524"
			sh $nvdl/$pkg -x
		fi
		nvrepls=`ls $nvtmp | grep 'NVIDIA'`
		nvrep=`echo ${nvrepls%/}`; 
		ls -v $nvtmp | grep '$(version_id)'
			if [[ -d $nvtmp/$nvrep ]] ; then
				echo -e "\f$r>>$y $nvrep ready ! $r <<$y\n" ; sleep 2
			else
				echo -e "\f$j $nvrep $msg525$y\n" ; sleep 1
				rm -f -R $nvtmp/* ; menu
			fi
		do_tar
		rm -f -R $nvtmp/* ; menu
		menu
	else
	    echo -e "$y $msg526$j $(version_id)$y $mqg527$v\n"
	    echo -e "$v $msg528\n"
	    rm -f -R $nvtmp/* ; menu
	fi
}

### RESTART X SESSION
function dm_restart(){
	if [[ -e /tmp/.X0-lock ]] ; then
	    if [[ -e /var/run/gdm.pid ]] ; then
		gdm-stop ; gdm
	    else
		killall gdm-binary ; gdm
	    fi
	else
	    killall gdm-binary
	    modprobe -r nvidia ; depmod -qa
	    modprobe nvidia
	    gdm
	fi
}
### UNINSTALL fUnction
function uninstall(){
	echo -e "\n$r --->$y     $msg531$r     <---$y\r"
	sh $nvdir/nvidia-installer --uninstall
	echo -e "\f$v $msg532 \n" ; sleep 2
	menu
}
### MENU MENU
function menu(){
echo -e "$v\n $msg601$j\r"
PS3=$( echo -e "\n$v $ans002$j ==>")
select choice in "$A" "$B" "$C" "$D" "$E" "$F" "$G" "$H" "$I"; do 
	case $choice in
		"$A")
			echo -e "$v--->  $msg602  <---\r" ; from_archive
		;;
		"$B")
			echo -e "$v--->  $msg603  <---\r" ; from_directory	
		;;
		"$C")
			echo -e "\n$r--->$v  $msg604$r  <---$v\n" ; from_net
		;;
		"$D")
			echo -e "\n$r--->$v  $msg605$r <---\n$y" ; archive
		;;
		"$E")
			echo -e "$v--->  $msg606 <---\r" ; uninstall
		;;
		"$F")
			echo -e "$v $msg607 "
			sleep 2 ; /usr/sbin/gdm-stop ; exit 1
		;;
		"$G")
			echo -e "$v $msg608 "
			sleep 2 ; dm_restart ; exit 0
		;;
		"$H")
			echo -e "$v\t $msg609 ... \f"
			check_update ; menu #; rm -f $nvtmp/drvlist  $nvtmp/last_up
		;;
		"$I")
			echo -e "$t\n" ; exit 0
		;;
	esac
done
}
function first_start(){
	if [ "$LG" == "EN" ] || [ $2 == 'EN' ] ; then
			PACK=EN_PACK
	elif [ "$LG" == "FR" ] || [ $2 == 'FR' ] ; then
			PACK=FR_PACK
	fi
## language pack
#. $nvdir/locale/$PACK
. /home/$USER/Dev/svn/nvidia-maker-installer/$PACK
### FIRST START
	echo -e "\f\t$j $msg701 \r"
	echo -e "\t$j $msg702 \f" ; sleep 1
	echo -e "\f$v $msg703\r"
	echo -e " $msg704 \r"
	echo -e " $msg705\n"
## Creation of necessary directories in case they arent already there.:
	distro
	echo -e "$v  $msg706\t\t $j$DISTRO$v \n  $msg707\t $j$ARCH$v \n  $msg708\t\t$j$board \n"
	echo -e "$v  $msg709\n"
	if [ -d $nvdir ] ; then
		mkdir -p $nvtar $nvtmp $nvlog $nvupdate $nvdl
	fi
	if [[ $(ls -l $nvdl/* | sed -n '/\-rwxr\-xr\-x/p' | cat -n | sort -r | sed -n '1p'| awk '{print $1}') != $(ls -l $nvdl/* | cat -n | sort -r | sed -n '1p'| awk '{print $1}') ]] ; then
		chmod 755 $nvdl/*
	fi
	#if [[ "$(ls $nvtmp/drvlist)" == '' ]] || [[ "$(ls $nvtmp/last_update)" == '' ]]; then 
	#	touch $nvtmp/last_update
	#	touch $nvtmp/drvlist
	#fi
	## dep control
	dep_control
	presence=$( ls -Cd $nvdir )
	ls -Cd $nvdir > /dev/null
	if [ $? -eq 0 ] ; then
		echo -e "$j  ${presence#*/}$v clean. $msg710" #; ls $nvdir
		xlocksession
		else
		if [ $? -ne 0 ] ; then 
			echo -e "$y\f  $msg711\f"
			echo -e "$v  $msg712"
			echo -e "$v  $msg713\n   $y==>$j \c"
			read -e sourcedir
			mkdir -p $sourcedir/NVIDIA
				if [ $? -eq 0 ] ; then
				query='nvdir=\"'
					cat $0 | grep "$query" | sed -i 's/'$nvdir'/'$sourcedir/NVIDIA'/' $0
					#sed -i "29cnvdir=\"$sourcedir/NVIDIA\"" /usr/local/bin/nvidia-maker-*.sh
				fi
		fi
	fi
	menu
}
### SCRIPT INTRO
while getopts "x-t-h-c-d" OPTS ; do
	EXEC=/home/$USER
	if [ $2 == 'EN' ] ; then
		LG='EN'
	elif [ $2 == 'FR' ] ; then
		LG='FR'
	else
		echo -e "$r no language pack chosen\n EN = english\n FR = Français.$t"
		exit 0
	fi
case $OPTS in
	x|-xterm-ui)
		GEO=80x28
		xterm -sb -b 5 -bg black -bd black -bw 0 -geometry $GEO -e $EXEC/$0 -t $LG ##
	;;
	t|'-terminal')
		ID ; arch ; first_start
	;;
	XC)
		opts2 ; ID ; arch ; first_start
	;;
	h|'-help')
		echo -e "$(basename $0)$msg801" #"nvidia_maker_install.sh usage :
		echo -e "$msg802" #\n\t -t, --terminal \t exec installer in terminal mode."
		echo -e "$msg803" #"\t -x, --xterm_ui \t exec in a pretty xterm interface. \n\t -h, --help \t\t this help."
		echo -e "$msg804"
		exit 1
	;;
	c) # function test command (debug only)
		ARCH="x86_64"
		ip_net
		echo -e "$y$ip $j$iface par $net_ip$t"
		#echo -e "\f$v $msg111 $(echo $ip) $msg112 $(echo $iface) $msg113 \n$t"
	;;
	d) # function test command (debug only)
		ARCH="x86_64"
		ping_net
		echo -e "$y$connection$t"
		exit 0
	;;
	*)
		$EXEC/$0 -h
		exit 0
	;;
esac
done
exit 0